//
//  WBNetwoekManager+Extension.swift
//  Weibo
//
//  Created by kobe on 2016/12/24.
//  Copyright © 2016年 kobe. All rights reserved.
//

import Foundation

extension WBNetworkManager {
    
    
    
    /// 加载微博数据字典数组
    ///
    /// - parameter completion: 完成回调(list:微博字典数组)
    func statusList(sice_id:Int64 = 0,max_id:Int64 = 0,completion:@escaping (_:[[String:Any]]?,_:Bool)->()) {
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        
        let params = ["since_id":"\(sice_id)","max_id":"\(max_id > 0 ? max_id - 1 : 0)"]
        
        
        
        tokenRequest(method: .GET, URLString: urlStr, parameters: params) { (json, isSuccess) in
            
            print(json)
            //从json中获取字典数组  如果as失败 result = nil
            let result = (json as? [String:Any])?["statuses"] as? [[String:Any]]
            completion(result, isSuccess)
        
        }
    }
    
    //微博的未读数量
    func unreadCount(completion:@escaping (_ count:Int) -> ()) {
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlStr = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["uid":uid]
        
        tokenRequest(URLString: urlStr, parameters: params) { (json, isSucess) in
//            print(json)
            let dict = json as? [String:Any]
            let count = dict?["status"] as? Int
            completion(count ?? 0)
            
        }
        
    }
    
}

// MARK: - OAuth 相关方法
extension WBNetworkManager {
    
    /// 加载token
    
    
    /// jiaztoken
    ///
    /// - parameter code:       授权码
    /// - parameter completion: 完成回调[是否成功]
    func loadAccessToken(code:String,completion:@escaping (_ isSuccess:Bool) -> ()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id":WBAppKey,
                      "client_secret":WBAppSecret,
                        "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":WBRedirectURI]
        
        //发起网络请求
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            print(json)
            
            //直接用字典设置userAccount
            self.userAccount.yy_modelSet(with: (json as? [String : Any]) ?? [:])
            
            
            self.userInfo(completion: { dict in
                
                //使用用户信息字典设置用户账户信息
                self.userAccount.yy_modelSet(with: dict)
                
                //保存模型
                self.userAccount.saveAccount()
                
                
                //用户信息加载完成再完成回调
                completion(isSuccess)
            })
            
            
            
        }
    }
    
    
    func userInfo( completion:@escaping (_ dict:[String:Any]) -> ()) {
        let urlStr = "https://api.weibo.com/2/users/show.json"
        guard let uidStr = userAccount.uid else {
            return
        }
        let params = ["access_token":WBUserAccount().access_token ?? "","uid":uidStr] as [String : Any];
        
        tokenRequest(URLString: urlStr, parameters: params) { (json, isSuccess) in
            print(json)
            
            completion((json as? [String:Any]) ?? [:])
            
        }
    }
}
